<?php
require_once(str_replace("\\", "/", dirname(__FILE__)) . "/../config.inc.php");
$type = $_GET["type"];
$market = $_GET["market"];
switch($type){
case "stock":
    exportStocks($type, $market);
    break;
}

function exportStocks($type, $market){
    global $context;
    $stockManger = $context->getBean("modules.finance.business.stock");
    $stocks = $stockManger->getStocks($market);
    $filename = date("Y-m-d") . ".csv";
    if($market){
        $filename = "stocks_" . $market . "_" . $filename;
    }else{
        $filename = "stocks_" . $filename;
    }
    /*- 输出CSV表头 -*/
    $csv = "\xEF\xBB\xBF";
    $csv .= "股票代码,股票名称,证券市场\r\n";
    foreach($stocks as $stock){
        $csv .= $stock["symbol"] . "," . $stock["name"] . "," . $stock["market"] . "\r\n";
    }
    Header("Content-type: application/octet-stream; charset=utf-8");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: " . strlen($csv));
    Header("Content-Disposition: attachment; filename=" . $filename);
    echo $csv;
}
?>
